// pages/complaint/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    columns: [],
    columnsArr: [],
    show: false,
    showText: true,
    nowChose: {
      value: '请选择',
      index: 0
    },
    introduce: '',
    contacts: '',
    phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    util.http({
      url: 'api/miniwechat/center/category',
      data: {
        type: "Feedback"
      }
    }).then(res => {
      // console.log(res)
      let arr = ['请选择']
      res.reverse().forEach((item, index) => {
        arr.push(item.name)
      })

      that.setData({
        columns: arr,
        columnsArr: res
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },
  imgChange() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        util.postImg({
          url: 'api/miniwechat/index/upload',
          file: tempFilePaths[0]
        }).then((imgData) => {
          let url = JSON.parse(imgData).data.url;
          that.data.imgList.push(url)
          that.setData({
            imgList: that.data.imgList
          })
        }).catch((err) => {
          wx.showToast({
            title: String(err),
            icon: 'none',
            duration: 2000
          })
        })
      }
    })
  },
  deleteImg(e) {
    this.data.imgList.splice(e.currentTarget.dataset.index);
    this.setData({
      imgList: this.data.imgList
    })
  },
  onClose() {
    this.setData({
      showText: true,
      show: false
    })
  },
  isShow() {
    this.setData({
      showText: false,
      show: true
    })
  },
  onConfirm(e) {
    this.setData({
      nowChose: e.detail
    })
    this.onClose();
  },
  introduceInput(e) {
    this.setData({
      introduce: e.detail.value
    })
  },
  contactsInput(e) {
    this.setData({
      contacts: e.detail.value
    })
  },
  phoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  onSubmit() {
    let phone = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0-8])|(18[0-9])|166|198|199|(147))\d{8}$/;
    if (this.data.nowChose.index === 0) {
      wx.showToast({
        title: '未选择反馈类型',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.introduce === '') {
      wx.showToast({
        title: '情况说明为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.imgList.length === 0) {
      wx.showToast({
        title: '图片未上传',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.contacts === '') {
      wx.showToast({
        title: '联系人为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.phone === '') {
      wx.showToast({
        title: '联系电话为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!(phone.test(this.data.phone))) {
      wx.showToast({
        title: '手机号码输入不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    util.http({
      url: 'api/miniwechat/center/feedback',
      method: 'POST',
      data: {
        type_id: this.data.columnsArr[this.data.nowChose.index - 1].id,
        introduce: this.data.introduce,
        images: this.data.imgList,
        contacts: this.data.contacts,
        phone: this.data.phone
      }
    }).then(res => {
      wx.showToast({
        title: '反馈成功',
        icon: 'none',
        duration: 1000
      })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1
        })
      }, 1000)
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  }
})